# @Time : 2021/10/26 14:15
# @Author : Li Kunlun
# @Description : 进程锁

import multiprocessing as mp
import time


def job(v, num, l):
    l.acquire()
    for _ in range(4):
        time.sleep(0.1)
        # 两个进程对共享变量进行操作，没有加锁的会有冲突
        v.value += num  # 获取共享变量值
        print(v.value)
    l.release()


def multicore():
    l = mp.Lock()  # 定义一个进程锁
    v = mp.Value('i', 0)  # 定义共享变量
    p1 = mp.Process(target=job, args=(v, 1, l))
    p2 = mp.Process(target=job, args=(v, 3, l))
    p1.start()
    p2.start()
    p1.join()
    p2.join()


if __name__ == '__main__':
    multicore()

"""
控制台输出：
    1
    2
    3
    4
    7
    10
    13
    16
"""
